package leetcode

/*
 * @lc app=leetcode.cn id=59 lang=golang
 *
 * [59] 螺旋矩阵 II
 */

// @lc code=start
func generateMatrix(n int) [][]int {
	ret := make([][]int, n)
	for r := range ret {
		ret[r] = make([]int, n)
	}

	for i, c := 1, 0; c < (n+1)/2; c++ {
		right, bottom := n-1-c, n-1-c
		for j := c; j <= right; j++ {
			ret[c][j] = i
			i++
		}
		if n % 2 == 1 && c == n/2 {
			break
		}
		for j := c + 1; j <= bottom; j++ {
			ret[j][right] = i
			i++
		}
		for j := right-1; c <= j; j-- {
			ret[bottom][j] = i
			i++
		}
		for j := bottom - 1; c < j; j-- {
			ret[j][c] = i
			i++
		}
	}

	return ret
}

// @lc code=end
